let mysql = require('mysql2/promise');
let createSqlTable = require('../model/admin/createdSql.js')
let unililty = require('./unility')

//创建数据库
exports.createSql = async function (params) {
    try {
        let connection = await mysql.createConnection({
            host: params.sqlAddress,
            user: params.sqlUser,
            password: params.sqlPwd
        });
        //创建数据库
        await connection.query('CREATE DATABASE  IF NOT EXISTS  ' + params.sqlName + ';')
        connection.end();
        //创建数据库管理员表
        await createSqlTable.createdAdmin();
        //创建数据库备份表
        await createSqlTable.createdBackUpSql();
        //创建公司信息表
        await createSqlTable.createCompany();
        //创建部门信息表
        await createSqlTable.createDepartment();
        //创建员工信息表
        await createSqlTable.createEmployees();
        //创建设备表
        await createSqlTable.createEquipment();
        //创建登录token 表
        await createSqlTable.createLoginToken();
        //创建消息通知表
        await createSqlTable.createNotice();
        //创建操作日志表
        await createSqlTable.createOperationLog();
        //创建版本表
        await createSqlTable.createVersion();
        //插入备份设置数据
        await createSqlTable.insertBackUp();
        //生成管理员信息
     
        let passwordMD5 = await unililty.encryptPWD(params.userPwd);
        await  createSqlTable.createAdminInfo(params.userName,passwordMD5);
        //生成公司信息
        await createSqlTable.createCompanyInfo(params.companyName);
        return true;
    } catch (err) {
        console.log(err);
    }
}